<template>
 
<el-main >
  <el-row :gutter="1">
    <div style="text-align: right;"><el-link icon="el-icon-edit" @click="editNews()">发布商品</el-link></div>
    <el-col :span="12" v-for="(item,index) in information.data" :key="index">
      <div class="grid-content bg-purple">


          <div slot="header" class="clearfix">
            <!-- 头像 -->
            <el-avatar :size="42" :src="item.user.headImg" style="float: left;"></el-avatar>
            <!-- 用户信息 -->
            <span style="position: relative;left: 8px;">
              <!-- 昵称 -->
              <span>{{item.user.nickname}}</span>
              <!-- 如果是当前用户发布的 -->
              <span v-if="user.id==item.sellUid" style="color:rgb(144,147,153);font-size:14px;">（个人发布）</span>
              <span v-else style="color:rgb(144,147,153);font-size:14px;">（其它用户发布）</span>
              <!-- 发布时间和小区信息 -->
              <span style="color:rgb(144,147,153);font-size:12px;">{{item.releaseTime}} </span>
              
            </span>
            
          </div>
           <el-divider></el-divider>
           

           <template>
              <el-button type="text" @click="open(item)">{{item.title}}</el-button>
          </template>
          <div v-if="item.photos">
              <el-image 
                style="width: 300px; height: 150px;margin-left: 2px;margin-right: 2px;"
                :src="item.photos.split(',')[0]" 
                :preview-src-list="item.photos.split(',')">
              </el-image>
          </div>
          
          <div>价格：{{item.price}}</div>
          
          <!-- 订单对话框 -->
          <span v-if="user.id!=item.sellUid&item.store!=0" style="color:rgb(144,147,153);font-size:14px;">
            <el-button><i class="el-icon-sell" @click="wantBuy(index)">可购买</i></el-button>
              </span>
           <el-button type="text" @click="clickOk(index)">
            <span v-if="item.secondOk==1">
              <i class="el-icon-star-on"></i>
              取消点赞({{item.okSum}})
            </span>
            <span v-else>
              <i class="el-icon-star-off"></i>
              点赞({{item.okSum}})
            </span>
          </el-button>

           <el-popover
            placement="right"
            title="评论详情"
            width="400"
            trigger="click">
            <!-- 评论按钮 -->
            <el-button slot="reference" type="text">
              <i class="el-icon-chat-line-square"></i>
              评论({{item.views}})
            </el-button>
            
            <el-input v-model="comment1" placeholder="说点什么？" size="small" style="width:350px;"></el-input>
            <el-button type="primary" icon="el-icon-edit" size="small" plain @click="comment(index)"></el-button>

            <!-- 评论for遍历展示 -->
            <div v-for="(comment, i) in item.secondHandComments" :key="i" style="margin-top:10px;border-bottom:1px solid rgb(230,230,230);">
              <el-avatar :size="40" :src="comment.user.headImg" style="float: left;"></el-avatar>
              <!-- 用户信息 -->
              <div style="position: relative;left: 50px;">
                <!-- 昵称和正文 -->
                <span style="position: relative;right: 40px;">
                  {{comment.user.nickname}}
                  <br>
                  <span style="font-size:16px;">
                    {{comment.content}}
                  </span>
                  <br>
                </span>
                <!-- 发布时间 -->
                <span style="color:rgb(144,147,153);font-size:12px;">
                  {{comment.releaseTime}}
                </span>
              </div>
            </div>
           </el-popover>
      </div>
      
      </el-col>

  </el-row>

  <!-- 分页组件，页数小于等于1时不显示 -->
      <el-col>
        <el-pagination
          background
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
          :page-sizes="[6, 12, 18, 24, 26]"
          :page-size="information.size"
          :total="information.total"
          :hide-on-single-page="hidePagination"
          layout="prev, pager, next, sizes, total">
        </el-pagination>
      </el-col>
    
      <el-dialog
        title="请填写订单信息"
        :visible.sync="dialogVisible"
        center
        width="50%">
            <div style="text-align:center;">{{orderHand.title}}</div>
            <div></div>
         

            <!-- 商品数量 -->
            <el-form :model="newOrder" ref="newOrder" label-width="100px" class="demo-ruleForm">
              <el-form-item
                label="商品数量"
                prop="num"
                :rules="[
                  { required: true, message: '商品数量为空'},
                  { type: 'number', message: '商品数量为数字值'}
                ]"
              >
                <el-input type="text" v-model.number="newOrder.num" autocomplete="off"></el-input>
              </el-form-item>
                <!-- 添加地址 -->
            <div>请选择地址</div>
            <div style="text-align:center;">
                <template >
              <el-table
                ref="singleTable"
                :data="address"
                align="center"
                highlight-current-row
                @current-change="handChange"
                style="width: 70%">
               
                
                <el-table-column
                  property="name"
                  label="姓名"
                  width="120">
                </el-table-column>
                <el-table-column
                  property="phone"
                  label="手机号"
                  width="120">
                </el-table-column>
                <el-table-column
                  property="address"
                  label="地址">
                </el-table-column>
              </el-table>
            </template>
            </div>
              <el-form-item>
                <el-button @click="dialogVisible = false" >取 消 订 单</el-button>
                <el-button type="primary" @click="submitForm('newOrder')">生 成 订 单</el-button>
                
              </el-form-item>

            </el-form>
             

        
      </el-dialog>
      
</el-main>
    
 
</template>

<script>
  
  export default{
      data(){
        return{
          information:{

          }, //最上面展示的图片
         hidePagination: true ,
         comment1:"",
          dialogVisible: false,
           delivery: false,
         user:"",
         address:{},
         orderHand:{},
          newOrder:{
            num: 0,
            gid:0
           },
           currentRow: '',
        


        }
      },
      methods:{
       handleCurrentChange:function(page){
          let size =this.information.size;
          console.log(size,page);
          this.$axios.get("http://localhost:8080/second/findNewGoods/"+page+"/"+size).then(res=>{
          
          if(res.data.status= "SUCCESS"){
            console.log(res.data.data);
            this.information=res.data.data
          }else{
            alert("最新商品获取失败")
          }
        })
       } ,
       handleSizeChange:function(size){
          let page =this.information.currentPage;
          console.log(size,page);
          this.$axios.get("http://localhost:8080/second/findNewGoods/"+page+"/"+size).then(res=>{
          
          if(res.data.status= "SUCCESS"){
            console.log(res.data.data);
            this.information=res.data.data
          }else{
            alert("最新商品获取失败")
          }
       })
      },
      clickOk:function(index){
        console.log("http://localhost:8080/second/ok/" + this.information.data[index].id);
        this.$axios.get("http://localhost:8080/second/ok/" + this.information.data[index].id).then(res => {
          console.log(res);
          if(res.data.status == "SUCCESS"){
            this.information.data[index].okSum ++
            this.information.data[index].secondOk = 1
            return
          }
          if(res.data.status == "FAIL"){
            this.information.data[index].okSum --
            this.information.data[index].secondOk = 0
          }else{
            this.$message.error("操作失败")
          }
      })
      },
      comment:function(index){
         if(this.comment1==""||this.comment1==null){
          this.$message.error("不可以评论空的内容哦~")
          console.log("评论为空");
          return
        }
       this.$axios.get("http://localhost:8080/second/addComment/"+this.information.data[index].id+"/"+this.comment1).then(res=>{
        if(res.data.status= "SUCCESS"){
            this.$message.success("评论成功，刷新后可见")
            /* let length =this.information.data[index].secondHandComments.length
            this.information.data.views++ */
         /*  console.log(res.data.data); */
            
            /* console.log("前"+JSON.stringify(this.information.data[index].secondHandComments));
             */
            /* this.information.data[index].secondHandComments.splice(length,0,res.data.data) */
            /* console.log("后"+JSON.stringify(this.information.data[index].secondHandComments)) */
          }else{
            this.$message.error("评论失败")
          }
       })  

      },
      
      editNews:function(){
         this.$router.push("/newSecondHand")
      },
      /* 商品详情 */
      open:function(item){
        let str = JSON.stringify(item);
        this.$router.push({ path: "/secondHandDetail", query: { str } });
      },
      wantBuy:function(index){
          this.newOrder.num=''
          this.currentRow=''
          this.orderHand= this.information.data[index]
          this.newOrder.gid=this.information.data[index].id
          this.$axios.get("http://localhost:8080/address/find").then(res=>{
          
          if(res.data.status= "SUCCESS"){
            console.log(res.data);
            this.address=res.data.data
            this.dialogVisible=true
            
             
            
          }else{
            alert("地址获取失败")
          }
          
        })
        this.orderHand=this.information.data[index]
        

      },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if(this.newOrder.num<1){
               this.$message.error("数量太小")
               return false;
            }
            if(this.newOrder.num>this.orderHand.store){
              this.$message.error("数量太大")
              return false;
            }


            if(this.newOrder.gid!=''&this.currentRow!=''){
               this.$axios.post("http://localhost:8080/second/addSecondOrder/"+this.newOrder.gid+"/"+this.newOrder.num+"/"+this.currentRow).then(res=>{
          
                  if(res.data.status= "SUCCESS"){
                     this.$message.success("订单生成成功")
                     this.$router.push( "/account/mySecondHandOrder");
                  }else{
                    this.$message.success("订单生成失败")
                  }
                  
                  })
              
            }else{
              this.$message.error("请选择地址")
            }
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },

     
      /* 地址相关方法 */
       setCurrent(row) {
        this.$refs.singleTable.setCurrentRow(row);
      },
      handChange:function(val){
          console.log(val.id);
          this.currentRow=val.id
      }
      },
      mounted(){
        this.user=JSON.parse(sessionStorage.getItem('user'))
        this.$axios.get("http://localhost:8080/second/findNewGoods/1/6").then(res=>{
          
          if(res.data.status= "SUCCESS"){
            console.log(res.data.data);
            this.information=res.data.data
            this.orderHand=res.data.data.data[0]
          }else{
            alert("最新商品获取失败")
          }
          
        })
      

        
      }
  }
</script>

<style scoped>
body{padding-right:0px!important}
.el-popup-parent--hidden {
  overflow: initial !important;
}
</style>